// pages/index/hotMore/hotMore.js
const tools = require('../../../utils/tool')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    next_page_url: '',
    url: '/api/applets/article/official', //第一页url
    hot: [],
    page:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      second_cate_id: options.second_cate_id
    })
    this.getHotMore('/aty/applet/a/article/hot', options.second_cate_id)
  },
  // 获取热门更多
  getHotMore(url, second_cate_id) {
    tools.showLoading()
    let getMore = tools.request(url, 'get', {
      page:this.data.page,
      size:5,
      second_cate_id: second_cate_id
    }, wx.getStorageSync('token'))
    getMore.then(res => {
        wx.hideLoading()
        console.log(res.statusCode)
        if (res.statusCode==200) {
          let hot = this.data.hot.concat(res.data.content)
          // hot = tools.formatting(hot)
          this.setData({
            hot,
            page:++this.data.page,
            totalElements:res.data.totalElements
         
          })
        } else {
          wx.showToast({
            title: res.data.error_msg,
            icon: "none"
          })
        }
        console.log(res)
      })
      .catch(err => {
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
      })
  },

  onReachBottom: function () {
    if(this.data.hot.length<this.data.totalElements){
      this.getHotMore('/aty/applet/a/article/hot', this.data.second_cate_id)
    }
    
  },

  //分享
  onShareAppMessage: function (res) {
    let imgurl = res.target.dataset.imgurl
    let that = this
    return {
      title: res.target.dataset.title,
      path: res.target.dataset.id,
      imageUrl: imgurl
    }
  },

  onShow() {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    let json = currPage.data.mydata;
    let hot = this.data.hot
    if (json.isZan) {
      hot.forEach((item, i) => {
        if (json.id == hot[i].id) {
          this.setData({
            [`hot[${i}].is_good`]: json.state,
            [`hot[${i}].click_num`]: json.ZanCount
          })
        }
      })
    }
    if (json.collect) {
      hot.forEach((item, i) => {
        if (json.id == hot[i].id) {
          this.setData({
            [`hot[${i}].is_collect`]: json.state,
            [`hot[${i}].collect_num`]: json.collect_num
          })
        }
      })
    }
  }
})